const Task = require("./index.js")
const request = Task.request
const newUrl = Task.newUrl
const proxyErrorHandler = Task.proxyErrorHandler

/**
 * 得到首页轮播图
 */
Task.prototype.getCarousel = function *(){
    let res = yield request.get(newUrl("carousels/all"))
    // proxyErrorHandler(res)
    return JSON.parse(res.text).msg
}

/**
 * 得到首页推荐课程
 */
Task.prototype.getIndexCourses = function *(){
    let res = yield request.get(newUrl("course/recommend"))
    // proxyErrorHandler(res)
    return JSON.parse(res.text).msg
}

/**
 * 得到课程列表数据
 * @param ctx
 * @returns {msg|{}|*}
 */
Task.prototype.getCourses = function *(){
    let ctx = this.ctx
    let querystring = ctx.request.querystring
    let res = yield request.get(newUrl("course/all",querystring))
    proxyErrorHandler(res)
    return res.body.msg
}

/**
 * 得到课程数据
 */
Task.prototype.getCourse = function *(){
    let ctx = this.ctx
    let courseId = ctx.params.courseId
    let res = yield request.get(newUrl(`course/specific/${courseId}`))
    proxyErrorHandler(res)
    return res.body.msg.course
}

/**
 * 得到评论数据
 */
Task.prototype.getComments = function *(){
    let ctx = this.ctx
    let courseId = ctx.params.courseId
    let res = yield request.get(newUrl("course/comments/all",`courseId=${courseId}&isNote=0`))
    proxyErrorHandler(res)
    return res.body.msg
}

/**
 * 得到笔记数据
 */
Task.prototype.getNotes = function *(){
    let ctx = this.ctx
    let courseId = ctx.params.courseId
    let res = yield request.get(newUrl("course/comments/all",`courseId=${courseId}&isNote=1`))
    proxyErrorHandler(res)
    return res.body.msg
}

/**
 * 附件数据
 * @param ctx
 */
Task.prototype.getFiles = function *(){
    let ctx = this.ctx
    let courseId = ctx.params.courseId
    let res = yield request.get(newUrl("admin/course/files/all",`courseId=${courseId}`))
    proxyErrorHandler(res)
    return res.body.msg
}

/**
 * 记录观看历史
 */
Task.prototype.recordHistory = function *(courseId,userId){
    let res = yield request.post(newUrl("user/course/history/add")).send({
        courseId,
        userId
    }).set('Accept', 'application/json')
    proxyErrorHandler(res)
    return res.body.msg
}

/**
 * 得到观看历史
 */
Task.prototype.getHistories = function *(userId){
    let res = yield request.get(newUrl("user/history/list",`userId=${userId}`))
    proxyErrorHandler(res)
    return res.body.msg
}

Task.prototype.getCollectCourses = function *(userId){
    let res = yield request.get(newUrl("user/course/collect/list",`userId=${userId}`))
    proxyErrorHandler(res)
    return res.body.msg
}

Task.prototype.getUserCommentsOrNotes = function *(userId,isNote){
    let res = yield request.get(newUrl("course/comments/all",`userId=${userId}&isNote=${isNote}`))
    proxyErrorHandler(res)
    return res.body.msg
}

